package com.match.competition_sub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.commonutils.Result;
import com.match.competition_sub.entity.Course;
import com.match.competition_sub.entity.StudentCourseNumber;
import com.match.competition_sub.entity.vo.CourseQuery;
import com.match.competition_sub.service.CourseService;
import com.match.competition_sub.service.StudentCourseNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AdiosF
 * @since 2021-09-21
 */
@Api(tags = "课程模块")
@RestController
@RequestMapping("/competition_sub/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private StudentCourseNumberService studentCourseNumberService;

    @ApiOperation("通过EasyExcel导入课程基本信息")
    @PostMapping("/addCourseByExcel")
    public Result addCourseByExcel(@RequestBody(required = true) MultipartFile file){
        //检验文件是否为空
        if (StringUtils.isEmpty(file)){
            return Result.error().message("请先选择好文件再上传");
        }
        courseService.addCourseByExcel(file,courseService);
        return Result.ok().message("课程信息录入成功");
    }


    @ApiOperation("添加课程")
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody(required = true) Course course){
        if (StringUtils.isEmpty(course)){
            return Result.error().message("提交的课程数据不能为空");
        }
        boolean save = courseService.save(course);
        if (!save){
            return  Result.error().message("课程信息存储失败");
        }else {
            return Result.ok().message("课程信息提交成功");
        }
    }


    @ApiOperation("根据课程Id删除课程信息")
    @DeleteMapping("/deleteCourseById/{courseId}")
    public Result deleteCourseById(@ApiParam(required = true,value = "课程ID")
                              @PathVariable(value = "courseId") String courseId){
        if (courseId==null){
            return Result.error().message("课程ID不能为空");
        }
        boolean b = courseService.removeById(courseId);
        if (b){
            return Result.ok().message("课程移除成功");
        }
        return Result.error().message("课程移除失败");
    }

    //TODO
    @ApiOperation("获取可以置换的课程信息")
    @PostMapping("/getCourseList")
    public Result getCourseList(@RequestBody CourseQuery courseQuery){
        if (courseQuery.getCourseTypeList().length>0){
            QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
            if (courseQuery.getCourseTypeList().length>0){
                courseQueryWrapper.in("course_type",courseQuery.getCourseTypeList());
            }
            if (!StringUtils.isEmpty(courseQuery.getAffirmCredit())){
                courseQueryWrapper.le("credit_hour",courseQuery.getAffirmCredit());
            }
            if (!StringUtils.isEmpty(courseQuery.getGrade())){
                courseQueryWrapper.eq("grade",courseQuery.getGrade());
            }
            if (!StringUtils.isEmpty(courseQuery.getDepartment())){
                courseQueryWrapper.eq("department",courseQuery.getDepartment());
            }
            if (!StringUtils.isEmpty(courseQuery.getMajorName())){
                courseQueryWrapper.eq("major_name",courseQuery.getMajorName());
            }
            //排除已经替换的课程
            if (!StringUtils.isEmpty(courseQuery.getStudentNumber())){
                //获取已经替换的课程代码
                QueryWrapper<StudentCourseNumber> studentCourseNumberQueryWrapper=new QueryWrapper<>();
                studentCourseNumberQueryWrapper.eq("stu_number", courseQuery.getStudentNumber()).select("course_number");
                List<StudentCourseNumber> list = studentCourseNumberService.list(studentCourseNumberQueryWrapper);
                List<String> stringList=new ArrayList<>();
                for (StudentCourseNumber studentCourseNumber : list) {
                    stringList.add(studentCourseNumber.getCourseNumber());
                }
                courseQueryWrapper.notIn("course_number",stringList);
            }
            List<Course> list = courseService.list(courseQueryWrapper);
            return Result.ok().data("courseList",list);

        }else {
            List<Course> list = null;
            return Result.ok().data("courseList",list);
        }
    }

    @ApiOperation("根据课程Id查询课程信息")
    @GetMapping("/getOneCourseById/{courseId}")
    public Result getOneCourse(@ApiParam(value = "课程Id")
                          @PathVariable(value = "courseId") String courseId){
        Course byId = courseService.getById(courseId);
        if (StringUtils.isEmpty(byId)){
            return Result.error().message("该课程已被移除");
        }
        return Result.ok().data("courseInfo",byId);
    }


    @ApiOperation("分页条件查询课程信息")
    @PostMapping("/findCorseByQueryPage/{current}/{limit}")
    public Result findCourseByQuery(@ApiParam(value = "当前页")
                               @PathVariable(value = "current")long current,
                                    @ApiParam(value = "当前记录数")
                               @PathVariable(value = "limit")long limit,
                                    @RequestBody CourseQuery courseQuery){
        Map<String,Object> map=courseService.findCourseByQueryPage(current,limit,courseQuery);
        long total = (long) map.get("total");
        List<Course> records = (List<Course>) map.get("records");
        if (total<=0){
            return  Result.ok().message("课程库暂无数据");
        }
        return Result.ok().data("total",total).data("records",records);
    }


    @ApiOperation("根据课程Id更新课程信息")
    @PostMapping("/updateByCourseId")
    public Result updateByCourseId(@RequestBody Course course){
        Course sqlCourse = courseService.getById(course.getId());
        //判断是否更改
        if (!sqlCourse.equals(course)){
            //校验课程代码
            if (!course.getCourseNumber().equals(sqlCourse.getCourseNumber())){
                QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
                courseQueryWrapper.eq("course_number",course.getCourseNumber());
                int count = courseService.count(courseQueryWrapper);
                if (count==1){
                    return Result.error().message("课程代码已存在");
                }
            }
        }else {
            return Result.error().message("请更改信息后操作");
        }
        courseService.updateById(course);
        return Result.ok().message("课程信息修改成功");
    }


    @ApiOperation("查找课程类别")
    @GetMapping("/getCourseType")
    public Result getCourseType(){
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        courseQueryWrapper.select("distinct course_type");
        List<Course> list = courseService.list(courseQueryWrapper);
        return Result.ok().data("courseList",list);
    }
}

